const path = require('path');


// express
const app = require('express')();
const fileUpload = require('express-fileupload');
app.use(fileUpload()); // for parsing multipart/form-data
app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control, Pragma');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  if (req.method === 'OPTIONS') {
    res.sendStatus(204);
  } else {
    next();
  }
});
// 上传文件
app.post('/upload', function (req, res) {
  if (!req.files) {
    return res.status(400).send('No files were uploaded.');
  }
  // save file
  // <input type="file" name="uploadFile" />
  let file = req.files.uploadFile;
  let encodeFileName = Number.parseInt(Date.now() + Math.random()) + file.name;
  file.mv(path.resolve(__dirname, '../static/upload/') + '/' + encodeFileName, function (err) {
    if (err) {
      return res.status(500).send({
        code: err.code,
        data: err,
        message: '文件上传失败',
      });
    }
    res.send({
      code: 0,
      data: {
        fileName: file.name,
        fileUrl: `http://localhost:3000/static/upload/${encodeFileName}`,
      },
      message: '文件上传成功',
    });
  });
});

// 获取文件
app.get('/static/upload/:fileName', function (req, res) {
  res.sendFile(path.resolve(__dirname, '../static/upload') + '/' + req.params.fileName);
});
// 获取im客服列表
app.get('/getIMServerList', function (req, res) {
  res.json({
    code: 0,
    data: Array.from(serverChatDic.values()).map((item) => {
      return item.serverChatEn;
    }), // 只需要serverChatDic.values内的serverChatEn
  });
});
app.listen(4000, () => {
  console.log('[app is running http://localhost:4000]');
});



// socket
var server = require('http').createServer();
var io = require('socket.io')(server);
var serverChatDic = new Map(); // 服务端
var clientChatDic = new Map(); // 客户端
io.on('connection', function (socket) {
  // 服务端上线
  socket.on('SERVER_ON', function (data) {
    let serverChatEn = data.serverChatEn;
    console.log(`有新的服务端socket连接了，服务端Id：${serverChatEn.serverChatId}`);
    serverChatDic.set(serverChatEn.serverChatId, {
      serverChatEn: serverChatEn,
      socket: socket,
    });
  });

  // 服务端下线
  socket.on('SERVER_OFF', function (data) {
    let serverChatEn = data.serverChatEn;
    serverChatDic.delete(serverChatEn.serverChatId);
  });

  // 服务端发送了信息
  socket.on('SERVER_SEND_MSG', function (data) {
    if (clientChatDic.has(data.clientChatId)) {
      clientChatDic.get(data.clientChatId).socket.emit('SERVER_SEND_MSG', { msg: data.msg });
    }
  });

  // 客户端事件；'CLIENT_ON'(上线), 'CLIENT_OFF'(离线), 'CLIENT_SEND_MSG'(发送消息)
  ['CLIENT_ON', 'CLIENT_OFF', 'CLIENT_SEND_MSG'].forEach((eventName) => {
    socket.on(eventName, (data) => {
      let clientChatEn = data.clientChatEn;
      let serverChatId = data.serverChatId;
      // 1.通知服务端
      if (serverChatDic.has(serverChatId)) {
        serverChatDic.get(serverChatId).socket.emit(eventName, {
          clientChatEn: clientChatEn,
          msg: data.msg,
        });
      } else {
        socket.emit('SERVER_SEND_MSG', {
          msg: {
            content: '未找到客服',
          },
        });
      }

      // 2.对不同的事件特殊处理
      if (eventName === 'CLIENT_ON') {
        // 1)'CLIENT_ON'，通知客户端正确连接
        console.log(`有新的客户端socket连接了，客户端Id：${clientChatEn.clientChatId}`);
        clientChatDic.set(clientChatEn.clientChatId, {
          clientChatEn: clientChatEn,
          socket: socket,
        });
        serverChatDic.has(serverChatId) &&
        socket.emit('SERVER_CONNECTED', {
          serverChatEn: serverChatDic.get(serverChatId).serverChatEn,
        });
      } else if (eventName === 'CLIENT_OFF') {
        // 2)'CLIENT_OFF'，删除连接
        clientChatDic.delete(clientChatEn.clientChatId);
      }
    });
  });
});
server.listen(4001, () => {
  console.log('[socket server is running http://localhost:4001]');
});
